package com.gxar.navigation.server.base.controller;

import com.gxar.common.entity.PageResult;
import com.gxar.common.entity.Result;
import com.gxar.navigation.server.base.model.node.vo.request.DistanceRequest;
import com.gxar.navigation.server.base.model.node.vo.request.NodeCreateRequest;
import com.gxar.navigation.server.base.model.node.vo.request.NodeUpdateRequest;
import com.gxar.navigation.server.base.model.node.vo.response.*;
import com.gxar.navigation.server.db.service.NodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author linsy
 * @version 1.0
 * @created 2023/9/12 14:06
 */
@Api(tags = "地图节点视图层")
@RestController
@RequestMapping("/api/map/node")
@RequiredArgsConstructor
@Slf4j
public class NodeController {

    private final NodeService nodeService;

    /**
     * 创建节点
     *
     * @param nodeCreateRequest 节点信息
     * @return 创建结果
     */
    @ApiOperation("创建节点")
    @PostMapping("/create/{active_id}")
    @ResponseBody
    public Result<NodeCreateResponse> create(@PathVariable(name = "active_id") Long activeId, @RequestBody @Valid NodeCreateRequest nodeCreateRequest) {
        NodeCreateResponse materialCreateResponse = nodeService.create(activeId, nodeCreateRequest);
        return Result.success(materialCreateResponse);
    }

    /**
     * 节点列表查询
     *
     * @param pageNum  当前页
     * @param pageSize 页记录数
     * @return 节点列表信息
     */
    @ApiOperation("节点列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page_num", value = "页码", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "page_size", value = "页记录数", dataType = "int", defaultValue = "10"),
    })
    @GetMapping("/list/{active_id}")
    public Result<PageResult<NodePageResponse>> pageList(@PathVariable(name = "active_id") Long activeId,
                                                         @RequestParam(name = "page_num", defaultValue = "1") int pageNum,
                                                         @RequestParam(name = "page_size", defaultValue = "10") int pageSize) {

        return nodeService.pageList(activeId, pageNum, pageSize);
    }

    /**
     * 获取节点详情
     *
     * @param id 节点id
     * @return 节点详情
     */
    @ApiOperation("查看节点详情")
    @ApiImplicitParam(name = "id", value = "节点ID", dataType = "long", required = true, paramType = "path")
    @GetMapping("/{active_id}/{id}")
    public Result<NodeInfoResponse> getDetail(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id) {
        return Result.success(nodeService.getDetail(activeId, id));
    }

    /**
     * 更新节点
     *
     * @param id                节点主键
     * @param nodeUpdateRequest 节点信息
     * @return 更新结果
     */
    @ApiOperation("更新节点")
    @ApiImplicitParam(name = "id", value = "节点ID", dataType = "long", required = true, paramType = "path")
    @PutMapping("/{active_id}/{id}")
    public Result<NodeUpdateResponse> update(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id, @RequestBody @Valid NodeUpdateRequest nodeUpdateRequest) {
        return Result.success(nodeService.update(activeId, id, nodeUpdateRequest));
    }

    /**
     * 删除节点
     *
     * @param id 节点id
     * @return 删除的节点id
     */
    @ApiOperation("删除节点")
    @ApiImplicitParam(name = "id", value = "节点ID", dataType = "long", required = true, paramType = "path")
    @DeleteMapping("/{active_id}/{id}")
    public Result<Boolean> delete(@PathVariable(name = "active_id") Long activeId, @PathVariable(name = "id") Long id) {
        return Result.success(nodeService.delete(activeId, id));
    }
    @ApiOperation("计算两个经纬度点的距离（单位：米）")
    @PostMapping("/distance")
    @ResponseBody
    public Result<DistanceResponse> distance(@RequestBody DistanceRequest request) {
        DistanceResponse response = nodeService.distance(request);
        return Result.success(response);
    }

}
